feat: add preemptive compaction with multi-phase context reduction #269
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Adds an opt-in preemptive compaction feature that proactively manages context before it exceeds model limits. When enabled, it monitors token usage after each message and triggers compaction when usage exceeds a configurable threshold (default: 85%).
Multi-Phase Compaction Flow
Features
message.updatedConfiguration
Disabled by default (opt-in):
{ "preemptiveCompaction": { "enabled": false, // opt-in "threshold": 0.85, // 85% context usage triggers compaction "cooldownMs": 60000, // 1 minute between compactions "minTokens": 50000, // minimum tokens before compaction can trigger "truncation": { "enabled": true, "protectedMessages": 3 // protect last 3 messages from truncation } } }Files Changed
lib/preemptive/- New preemptive compaction moduletypes.ts- Type definitionsconstants.ts- Constants and model context limitsstorage.ts- Truncation utilitiesmodel-limits.ts- Context limit inferenceindex.ts- Main handlerlib/config.ts- Added PreemptiveCompaction config interface and defaultsdcp.schema.json- Added preemptiveCompaction schemaindex.ts- Registered event hookREADME.md- DocumentationTesting
bun run build✓